/***************************************************************************
cmplx.c - COMPLEX NUMBERS USING MODULE


This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

Copyright (C) 2013  Bakhurin Sergey 
***************************************************************************/

#include <math.h>
#include "cmplx.h"
#include "dsp.h"


/***************************************************************************
ABS VALUE OF COMPEX NUMBER

Input parameters:
	complex a	- complex number

Return: 
	sqrt(a.re^2 + a.im^2)*/

typedsp absCmplx(complex a)
{
	return sqrt(a.re * a.re + a.im * a.im);	
}


/***************************************************************************
SQUARE ABS VALUE OF COMPEX NUMBER (a*conj(a))

Input parameters:
	complex a	- complex number

Return: 
	a.re^2 + a.im^2 */

typedsp absSqrCmplx(complex a)
{
	return a.re * a.re + a.im * a.im;	
}




/***************************************************************************
CONJUGATE COMPLEX NUMBERS

Input parameters:
	complex a	- complex number a.re+j*a.im   

Return: 
	a.re - j * a.im   */

complex conjCmplx(complex a)
{
	complex r = a;
	r.im = -r.im;
	return r;
}




/***************************************************************************
DIVIDING OF COMPLEX NUMBERS

Input parameters:
	complex a	- First complex number   
	complex b	- Second complex number   

Return: 
	a / b   */

complex divCmplx(complex a, complex b)
{
	complex r = mulCmplx(a, conjCmplx(b));
	typedsp a2 = absSqrCmplx(b);
	if(a2 > 0.0)
		a2 = 1.0/a2;
	else
		a2 = HUGE_VAL;
	r.re *= a2;
	r.im *= a2;
	return r;
}



/***************************************************************************
MULTIPLICATION OF COMPLEX NUMBERS

Input parameters:
	complex a	- First complex number   
	complex b	- Second complex number   

Return: 
	a * b  */

complex mulCmplx(complex a, complex b)
{
	complex r;
	r.re = a.re * b.re - a.im * b.im;
	r.im = a.im * b.re + b.im * a.re;
	return r;	
}




/***************************************************************************
COMPEX NUMBER CREATION

Input parameters:
	typedsp a	- real part
	typedsp b   - image part

Return: 
	a + j * b  */

complex numCmplx(typedsp a, typedsp b)
{
	complex r = {0.0, 0.0};
	r.re = a;
	r.im = b;
	return r;
}




/***************************************************************************
SUM OF COMPLEX NUMBERS

Input parameters:
	complex a	- First complex number   
	complex b	- Second complex number   

Return: 
	a + b	*/

complex sumCmplx(complex a, complex b)
{
	complex r;
	r.re = a.re + b.re;
	r.im = a.im + b.im;
	return r;
}
